package com.xl.studentpass.service.impl;

import com.xl.studentpass.entity.po.TaskOverViewPo;
import com.xl.studentpass.mapper.ImageMapper;
import com.xl.studentpass.mapper.IndexDataTaskTopMapper;
import com.xl.studentpass.service.IndexDataService;
import com.xl.studentpass.utils.ImageIdType;
import com.xl.studentpass.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.xl.studentpass.service.impl.AcceptTaskServiceImpl.getStrings;

/**
 * @author: xl
 * @createTime: 2023/5/27 16:21:17
 * @program: student-pass
 * @e-mail: 2199396150@qq.com
 * @description:
 */
@Service
@Slf4j
@CacheConfig(cacheNames = "taskInfo")
public class IndexDataServiceImpl implements IndexDataService {
    @Resource
    IndexDataTaskTopMapper indexDataTaskTopMapper;
    @Resource
    ImageMapper imageMapper;

    @Override
    //@Cacheable("index")
    public R getTopN(Integer pageNo, Integer pageSize) {
        Integer from = (pageNo - 1) * pageSize;
        log.info("查询数据" + "从 " + from + "到" + (from + pageSize));

        List<TaskOverViewPo> taskInfos = indexDataTaskTopMapper.getTopN(from, pageSize);
        taskInfos.forEach(x -> {
            x.setImageUrls(getImageUrls(x.getId(), ImageIdType.TASK));
        });
        return R.ok().put("taskTopn", taskInfos);
    }

    private List<String> getImageUrls(Integer id, ImageIdType idType) {
        return getStrings(id, imageMapper, idType);
    }

}
